import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import iView from 'view-design'
import { setTitle } from '@/libs/util'
import routes from '@/router/routers'

Vue.use(Router)
export const router = new Router({
  routes: routes,
  mode: 'hash'
})


router.beforeEach((to, from, next) => {

  let vuexMenu = store.state.vuex_routers.filter(item => item.name == to.name)


  store.commit('$uStore', {
    name: 'router_name', value: to.name
  })
  iView.LoadingBar.start()
  if (to.meta.title != "登录") {
    if (store.state && store.state.vuex_user && store.state.vuex_user.token) {
      if (to.fullPath != '/') {
        if (vuexMenu) {

          store.commit('$uStore', {
            name: 'vuex_menu', value: vuexMenu[0]
          })
        }
        next()
      } else {
        next({ path: '/404' })
      }
    } else {
      next({ path: '/login' })
    }
  } else {
    next()
  }
})

router.afterEach(to => {
  setTitle(to, router.app)
  iView.LoadingBar.finish()
  window.scrollTo(0, 0)
})

export default router
